﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BankSimulation
{
    //ATM类
    internal class ATM
    {
        
        //----------字段----------
        //ATM对应的银行
        CreditBank creditBank;

        //----------方法----------
        //构造函数
        public ATM (CreditBank bank)
        {
            this.creditBank = bank;
        }
        //先登录，获取账号和密码，通过账号密码获取account账户
        public void SignUp(string id1, string pwd1)
        {
            CreditAccount account = creditBank.GetAccount(id1, pwd1);
            if(account == null)
            {
                //TODO:提示重复了，先注释掉
                //MessageBox.Show("您的账号密码有误，请重新登陆！");
            }
            else
            {
                MessageBox.Show("登陆成功！");
                //将账户设置为当前登录的账户
                GlobalVar.accountCurrent = account;
                
            }
        }
        //查看余额
        public double AtmDisplayMoney(Account account, out string creditBalance)
        {
            //account是null会抛出异常,需进行异常处理
            try
            {
                creditBalance = GlobalVar.accountCurrent.CreditBalance.ToString();
                return account.Money;
            }catch(NullReferenceException)
            {
                MessageBox.Show("请先登录！");
                creditBalance = string.Empty;
                return -1;
            }
            
        }
        //ATM存钱
        public void AtmSaveMoney(double money)
        {
            //account是null会抛出异常,需进行异常处理
            try
            {
                GlobalVar.accountCurrent.SaveMoney(money);
            }
            catch(NullReferenceException)
            {
                //MessageBox.Show("AtmSaveMoney异常，向上抛给调用者！");
                //Console.WriteLine(e.Message);
                //此处不处理异常，向上抛给调用者
                throw;
            }
        }

        //ATM取钱
        public void AtmWithdrawMoney(double money)
        {
            //account是null会抛出异常，需进行异常处理
            try{
                //坏钞率百分之三十

                if (GlobalVar.random.Next(100) < 30)
                {
                    throw new BadCashException("有坏钞，ATM取钱异常！");
                }
            GlobalVar.accountCurrent.WithdrawMoney(money);

            }
            catch (NullReferenceException)
            {
                //此处不处理异常，向上抛给调用者
                throw;
            }
            catch(BadCashException e)
            {
                MessageBox.Show(e.Message);
            }


        }
    }
}
